<template>
    <div class="table">
            <div class="crumbs">
                <el-breadcrumb separator="/">
                    <el-breadcrumb-item><i class="icon iconfont icon-jifen"></i>&nbsp;秒杀活动商品设置</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="container">
                <el-form ref="form" label-width="160px">
                    <el-form-item label="时间段类型：">
                        <el-select v-model="value" placeholder="请选择时间段" style="width:400px">
                            <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="秒杀时间范围：">
                        <el-time-picker
                        format="HH:mm:ss"
                        value-format="HH:mm:ss"
                        is-range
                        v-model="value4"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        placeholder="选择时间范围" style="width:400px">
                    </el-time-picker>
                    </el-form-item>
                    
                    <el-form-item label="商品ID：">
                    <div class="clearfix">
                        <div v-for="(item,index) in skuProp" style="padding-bottom:10px;" :key="index">
                            <el-input v-model="item.id" type="number"  placeholder="请输入商品ID" style="width:400px" clearable></el-input>
                            <el-button type="danger" icon="el-icon-delete" @click="delGoodsSku(item,index)"></el-button>
                        </div>
                        <div style="padding-top:5px;">
                            <el-button type="primary" plain @click="addGoodsSku"><i class="el-icon-plus"></i>&nbsp;&nbsp;新增商品ID</el-button>
                        </div>
                    </div>
                </el-form-item>
                </el-form>
                <!-- 保存 -->
                <el-button class="save" type="primary" @click="onSubmit">保存</el-button>
                
            </div>
             <!-- 删除提示框 -->
            <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
                <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="delVisible = false">取 消</el-button>
                    <el-button type="primary" @click="deleteRow">确 定</el-button>
                </span>
            </el-dialog>
        </div>
</template>
<script>
import { getSeckillSet } from "@/api/goods/goods";
export default {
    name:"goodsHot",
    data (){
        return{
            fullscreenLoading:false,
            idx:0,//当前记录的下标
            delVisible:false,
            skuProp:[{id:''},{id:''},{id:''},{id:''}], //商品ID
            options: [{
                value: '1',
                label: '秒杀第一列时间段'
                }, {
                value: '2',
                label: '秒杀第二列时间段'
                }, {
                value: '3',
                label: '秒杀第三列时间段'
                }, {
                value: '4',
                label: '秒杀第四列时间段'
                }
            ],
            value: '1',
            time1:'',
            time2:'',
            value4:''
           
        }
    },
    methods:{
         //新增商品规格信息
        addGoodsSku(){
            var object = {id:''};
            this.skuProp.push(object);
        },
         //确定删除图片
        deleteRow(){

            this.skuProp.splice(this.idx,1);
            
            //console.log(this.bannerPicture)
            // this.tableData.splice(this.idx,1);
            this.$message.success('删除成功');
            this.delVisible = false;
        },
        delGoodsSku(item,index){
            if(this.skuProp.length<=1){
                this.$notify({title: '警告',message: '规格信息至少要填一项',type: 'warning'});
            }else{
                this.idx = index;
                this.delVisible = true;
            }
        },
        //校验规格
        toptipSkuFlag(){
            var flag = false;
            var reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
            var  stockReg= /^\+?[1-9][0-9]*$/;
            // console.log(this.skuProp)
            for (let index = 0; index < this.skuProp.length; index++) {
                if(this.skuProp[index].id==""){
                    this.$notify.info({title: '提示',message: '请填写商品ID'});
                    flag = true;
                    return false;//跳出循环
                }
            }
            return flag;
        },
        //确认设置
        onSubmit (){
            //console.log(this.selectedOptions[0]);
            // console.log(this.value4[0])
            if(this.value4 ==''){
                this.$notify.info({title: '提示',message: '请选择秒杀的开始与结束时间'});
                return false;
            }else if(this.toptipSkuFlag()){}
            else{
                var  goods = [];
                this.skuProp.forEach(row=>{
                    if(row.id!=""){
                        goods.push(row.id)
                    }
                    
                });
                const param ={
                        id:this.value,
                        startTime:this.value4[0],
                        endTime:this.value4[1],
                        goodsIdList:JSON.stringify(goods),

                    }
                    
                    this.onSubmitAjax(param);
            }
        },
        //提交到服务器中保存项目
        onSubmitAjax(param){

            getSeckillSet(param).then(response=>{
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，设置成功',
                        type: 'success'
                    });
                    setTimeout(function(){
                        this.value4='';
                        this.skuProp  =[{id:''},{id:''},{id:''},{id:''}];
                    },1500);
                }else{
                    this.$notify.error({
                        title: '设置失败',
                        duration:2000,
                        message: '失败原因：'+response.data.msg
                    });
                }
            })
            
        }
    }
}
</script>
<style lang="stylus" scoped>
    .save{
        margin-left :160px;
        width :150px;
    }
</style>


